<template>
  <van-nav-bar title="客户管理" fixed @click-right="router.go(-1)">
    <template #right>
      <van-icon name="cross" size="18" />
    </template>
  </van-nav-bar>
  <div class="search-wrapper">
    <van-search v-model="customerParams.keyword" show-action placeholder="姓名、手机号" @search="getCustomerList(1)">
      <template #action>
        <div class="search-btn" @click="getCustomerList(1)">搜索</div>
      </template>
    </van-search>
  </div>
  <div class="customer-index">
    <van-empty v-if="customerList.length === 0" image="search" description="没有搜索到客户信息哦~" />
    <template v-else>
      <van-cell-group>
        <van-cell v-for="item in customerList" :key="item.id" :title="`姓名：${item.nickName}`" @click="openDetail(item)">
          <template #title>
            <div class="name">姓名：{{ item.nickName }}</div>
          </template>
          <template #label>
            <div class="customer-item">
              <div class="mobile-wrapper">
                <div>手机号：{{ item.mobile }}</div>
              </div>
              <div class="time-wrapper">
                <div class="time-item">创建时间：{{ item.registryTime ? dayjs(item.registryTime).format('YYYY-MM-DD HH:mm:ss') : '' }}</div>
                <div class="time-item">更新时间：{{ item.updateTime ? dayjs(item.updateTime).format('YYYY-MM-DD HH:mm:ss') : '' }}</div>
              </div>
              <div class="assets-wrapper">
                <div>{{ formatAmountBySize(item.assets || 0) }}{{ formatAmountTextBySize(item.assets || 0) }}</div>
                <div>资产金额</div>
              </div>
            </div>
          </template>
        </van-cell>
      </van-cell-group>
      <van-pagination
        v-if="customerTotal > customerParams.rows"
        v-model="customerParams.page"
        :total-items="customerTotal"
        :items-per-page="customerParams.rows"
        :show-page-size="5"
        force-ellipses
        @change="getCustomerList"
      />
    </template>
  </div>
  <van-floating-bubble axis="xy" icon="plus" magnetic="x" @click="addCustomer" />

  <EditCustomer ref="EditCustomerRef" @refresh="getCustomerList" />
  <CustomerDetail ref="CustomerDetailRef" @refresh="getCustomerList" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { apiGetCustomerPage } from '@/api/customer'
import dayjs from 'dayjs'
import EditCustomer from './edit.vue'
import CustomerDetail from './detail.vue'
import { useRouter } from 'vue-router'
import { formatAmountBySize, formatAmountTextBySize } from '@/utils/common'

const router = useRouter()
const customerParams = ref({
  page: 1,
  rows: 10,
  keyword: ''
})

const customerList = ref([])
const customerTotal = ref(0)
const EditCustomerRef = ref()
const CustomerDetailRef = ref()

const getCustomerList = async (page = 1) => {
  customerParams.value.page = page
  apiGetCustomerPage(customerParams.value).then((res) => {
    customerList.value = res.list
    customerTotal.value = res.total
  })
}

const addCustomer = () => {
  EditCustomerRef.value.open({ state: 1 }, 'add')
}

const openDetail = (customer) => {
  CustomerDetailRef.value.open(customer)
}

onMounted(() => {
  getCustomerList()
})
</script>

<style lang="less" scoped>
.search-wrapper {
  position: fixed;
  top: var(--van-nav-bar-height);
  left: 0;
  right: 0;
  z-index: 1;
  .search-btn {
    padding-right: 4px;
  }
}
.customer-index {
  padding-top: 100px;
  .name {
    font-size: 16px;
    font-weight: 500;
  }
  .customer-item {
    position: relative;
    .mobile-wrapper {
      font-size: 16px;
      color: #323232;
      margin-top: 10px;
      margin-bottom: 10px;
    }
    .time-wrapper {
      font-size: 12px;
      display: flex;
      .time-item {
        flex: 1;
      }
    }
    .assets-wrapper {
      position: absolute;
      right: 0;
      top: -32px;
      border: 1px solid var(--van-cell-border-color);
      border-radius: 5px;
      padding: 5px 10px;
      text-align: center;
      font-weight: 600;
      width: 90px;
      color: #323232;
      font-size: 14px;
    }
  }
}
</style>
